package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class DiancanActivity extends AppCompatActivity {
    private String[] names ={"烤鸭","回锅肉","茄子","宫保鸡丁"};
    private String[] titles ={"烤鸭烤鸭烤鸭烤鸭烤鸭","回锅肉回锅肉回锅肉回锅肉","茄子茄子茄子茄子茄子","宫保鸡丁宫保鸡丁宫保鸡丁宫保鸡丁"};
    private String[] prices ={"88.0/一份","12.3元/一份","3.0元/一份","4.0元/一份"};
    private int[] icons ={R.drawable.one,R.drawable.two,R.drawable.tree,R.drawable.four};
    private ListView lv;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diancan_main);
        initView();
    }
    private void initView() {
        lv = findViewById(R.id.lv);
        MyBaseAdapter mAdapter = new MyBaseAdapter();
        lv.setAdapter(mAdapter);
    }
    class MyBaseAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return names.length;
        }
        @Override
        public Object getItem(int position) {
            return names[position];
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(DiancanActivity.this,R.layout.diancan_list,null);
            ImageView iv = view.findViewById(R.id.image);
            TextView name = view.findViewById(R.id.name);
            TextView title = view.findViewById(R.id.name2);
            TextView price = view.findViewById(R.id.price);
            iv.setBackgroundResource(icons[position]);
            name.setText(names[position]);
            title.setText(titles[position]);
            price.setText(prices[position]);
            return view;
        }
    }
}
